﻿using System;
using System.Windows.Forms;
using Moe.ECamel.Client.Controllers;
using Moe.ECamel.Client.WinForms.Common;
using Moe.ECamel.Common.Entities;
using Moe.ECamel.Common.Security;

namespace Moe.ECamel.Client.WinForms.Controls
{
	public partial class LibraryControl : ECamelUserControl
	{
		public LibraryControl()
		{
			InitializeComponent();
		}
		public LibraryControl(EntityDetailsController<Library> detailsController)
			: this()
		{
			this.LibraryDetailsControl.Initialize(detailsController);
			this.EmployeeListControl.Initialize(new EmployeesController(detailsController.Entity.LibraryId));
			this.CustomerListControl.Initialize(new CustomersController(detailsController.Entity.LibraryId));
			this.InventoryControl.Initialize(new ItemsController(detailsController.Entity.LibraryId));
			this.CustomersDebtsReportControl.Initialize(detailsController.Entity.LibraryId);
			this.TopRentedItemsReportControl.Initialize(detailsController.Entity.LibraryId);

			ApplyPermissions();
		}

		private void ApplyPermissions()
		{
			RemoveTabPageIfNotPermitted(LibraryDetailsTabPage, Operations.LibraryView);
			RemoveTabPageIfNotPermitted(LibraryEmployeesTabPage, Operations.EmployeeView);
			RemoveTabPageIfNotPermitted(LibraryCustomersTabPage, Operations.CustomerView);
			RemoveTabPageIfNotPermitted(LibraryInventoryTabPage, Operations.ItemView);
			RemoveTabPageIfNotPermitted(LibraryCustomersDebtsReportTabPage, Operations.ReportCustomersDebts);
			RemoveTabPageIfNotPermitted(LibraryTopRentedItemsReportTabPage, Operations.ReportTopRentedItems);
		}

		private void RemoveTabPageIfNotPermitted(TabPage tabPage, String operation)
		{
			if (!ECamelHelper.IsPermitted(operation))
				this.LibraryTabControl.TabPages.Remove(tabPage);
		}

		private void LibraryDetailsTabPage_Enter(object sender, EventArgs e)
		{
		}
		private void LibraryEmployeesTabPage_Enter(object sender, EventArgs e)
		{
			this.EmployeeListControl.RefreshView();
		}
		private void LibraryCustomersTabPage_Enter(object sender, EventArgs e)
		{
			this.CustomerListControl.RefreshView();
		}
		private void LibraryInventoryTabPage_Enter(object sender, EventArgs e)
		{
			//this.InventoryControl.RefreshView();
		}
	}
}
